package com.supcon.exchangedata.service.gj;

import com.supcon.exchangedata.pojo.entity.oracle.CtGjPbjh;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.SgjjtPbzyjhb;
import com.supcon.exchangedata.repository.master.MasterGjPbjhRepository;
import com.supcon.exchangedata.repository.slave4.SlaveGjBasicInfo6;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/29/16:20
 * @Description:
 */
@Service
@Slf4j
public class BusOtherDataService {
    @Resource
    private SlaveGjBasicInfo6 slaveGjBasicInfo6;
    @Resource
    private MasterGjPbjhRepository masterGjPbjhRepository;

    public void BusPbjhData(){
        List<SgjjtPbzyjhb> allGjpbjh = slaveGjBasicInfo6.findAllGjpbjh();
        if(!CollectionUtils.isEmpty(allGjpbjh)){
            masterGjPbjhRepository.deleteAll();
            List<CtGjPbjh> ctGjPbjhs = new ArrayList<>();

            for (SgjjtPbzyjhb origin : allGjpbjh) {
                CtGjPbjh ctGjPbjh = new CtGjPbjh();
                ctGjPbjh.setPlansid(origin.getPlansid());

                ctGjPbjh.setRouteName(origin.getRoutename());

                ctGjPbjh.setRouteNameChild(origin.getRoutenameChild());

                ctGjPbjh.setRouteId(origin.getRouteid());

                ctGjPbjh.setDuty(origin.getDuty());

                ctGjPbjh.setPlan(origin.getPlan());

                ctGjPbjh.setStartTime(origin.getStartTime());

                ctGjPbjh.setEndTime(origin.getEndTime());

                ctGjPbjh.setStartStation(origin.getStartStation());

                ctGjPbjh.setEndStation(origin.getEndStation());

                ctGjPbjh.setCarType(origin.getCarType());

                ctGjPbjh.setDutyMileage(origin.getDutyMileage());

                try {
                    ctGjPbjh.setGroupId(BigDecimal.valueOf(origin.getGroupId()));
                }catch (Exception e){
                    log.info("错误信息"+e.getMessage());
                }
                try {
                    ctGjPbjh.setService(BigDecimal.valueOf(origin.getService()));
                }catch (Exception e){
                      log.info("错误信息"+e.getMessage());
                }

                try {
                    ctGjPbjh.setNoService(BigDecimal.valueOf(origin.getNoService()));
                }catch (Exception e){
                    log.info("错误信息"+e.getMessage());
                }



                ctGjPbjh.setUpdateTime(LocalDateTime.now());
                ctGjPbjhs.add(ctGjPbjh);
            }

            masterGjPbjhRepository.saveAll(ctGjPbjhs);

        }
    }
}
